package com.infodms.dms.actions.report.reportOne;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.actions.repairOrder.RoMaintainMain;
import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.bean.ActMater;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.report.jcafterservicereport.SpecialCostReportDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmOrgPO;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;

public class SalesReport {
	private Logger logger = Logger.getLogger(RoMaintainMain.class);
	ActionContext act = ActionContext.getContext();
	AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
	SpecialCostReportDao dao=new SpecialCostReportDao();
	private final String BillReportUrl   = "/jsp/report/billTicket.jsp";
	private final String ActReportUrl   = "/jsp/report/actSales.jsp";
	private final String BillDetailReportUrl   = "/jsp/report/billDetailTicket.jsp";
	private final String ActDetailReportUrl   = "/jsp/report/actDetailSales.jsp";
	private final String BillDealerDetailReportUrl   = "/jsp/report/dealerBillDetaliTicket.jsp";
	private final String ActDealerDetailReportUrl   = "/jsp/report/dealerActDetaliSales.jsp";
	private final String ActDealerReportUrl   = "/jsp/report/actDealerSales.jsp";
	private final String BillDealerReportUrl   = "/jsp/report/billDealerTicket.jsp";
	private final String FleetSalesUrl="/jsp/report/fleetSalesCvs.jsp";
	private final String FleetSalesLogUrl="/jsp/report/fleetSalesLog.jsp";
	private final String FleetPreparCvsUrl="/jsp/report/fleetPreparCvs.jsp";
	private final String FleetFollowUrl="/jsp/report/fleetFollow.jsp";
	private final String FleetContractUrl="/jsp/report/fleetContract.jsp";
	private final String FleeActTjUrl="/jsp/report/fleetActTj.jsp";
	public void getBillTicketReport(){
		try {
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			areaList.size();
			String orgId=logonUser.getOrgId().toString();
			TmOrgPO po=new TmOrgPO();
			po.setOrgId(Long.parseLong(orgId));
			po.setDutyType(Constant.DUTY_TYPE_LARGEREGION);
			if(dao.select(po).size()==1){
				act.setOutData("orgId", orgId);
			}
			act.setOutData("areaList", areaList);
			act.setForword(BillReportUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getActSalesReport(){
		try {
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			areaList.size();
			String orgId=logonUser.getOrgId().toString();
			TmOrgPO po=new TmOrgPO();
			po.setOrgId(Long.parseLong(orgId));
			po.setDutyType(Constant.DUTY_TYPE_LARGEREGION);
			if(dao.select(po).size()==1){
				act.setOutData("orgId", orgId);
			}
			act.setOutData("areaList", areaList);
			act.setForword(ActReportUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	public void getBillMater(){
		try {
			RequestWrapper request = act.getRequest();
			String areaId=request.getParamValue("area_id");
			if(areaId!="" && areaId!=null){
			List<ActMater> maters=dao.selectAllMaters(areaId);
			StringBuffer mat=new StringBuffer("");
			for(int i=0;i<maters.size();i++){
 				String [] a=maters.get(i).getMaterId().split(",");
				for(int j=0;j<a.length;j++){
					mat.append(a[j]);
					if(i==(maters.size()-1)&&j==(a.length-1)){
					}else{
						mat.append(",");
					}
				}
			}
			System.out.println(mat);
			act.setOutData("mat", mat.toString());
			}else{
				act.setOutData("mat", 1);
			}
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	public void getDealerBillTicketReport(){
		try {
			StringBuffer str=new StringBuffer("");
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			areaList.size();
			String dealerId=logonUser.getDealerId();
			List<Map<String, Object>> dealerIds=dao.selectAllDealers(dealerId);
			for(int i=0;i<dealerIds.size();i++){
				str.append(dealerIds.get(i).get("DEALER_ID"));
				str.append(",");
			}
			if(dao.selectDealers(dealerId).size()>0){
				str.append(dealerId);
//				Map map=dao.selectDealers(dealerId).get(0);
//				String a=map.get("DEALER_ID").toString();
//				List map1=dao.selectMyDealers(a);
//				//String b=map1.get("ROOT_DEALER_ID").toString();
//				if(map1!=null && map1.size()>0){
//					act.setOutData("dealerId", dealerId);
//				}else{
					act.setOutData("mydealerId", str);
				//}
			}
			act.setOutData("areaList", areaList);
			act.setForword(BillDealerReportUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getDealerActSalesReport(){
		try {
			StringBuffer str=new StringBuffer("");
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			areaList.size();
			String dealerId=logonUser.getDealerId();
			
			List<Map<String, Object>> dealerIds=dao.selectAllDealers(dealerId);
			for(int i=0;i<dealerIds.size();i++){
				str.append(dealerIds.get(i).get("DEALER_ID"));
				str.append(",");
			}
			if(dao.selectDealers(dealerId).size()>0){
				str.append(dealerId);
//				Map map=dao.selectDealers(dealerId).get(0);
//				String a=map.get("DEALER_ID").toString();
//				List map1=dao.selectMyDealers(a);
//				//String b=map1.get("ROOT_DEALER_ID").toString();
//				if(map1!=null && map1.size()>0){
//					act.setOutData("dealerId", dealerId);
//				}else{
					act.setOutData("mydealerId", str);
				//}
			}
			act.setOutData("areaList", areaList);
			act.setForword(ActDealerReportUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getFleetSales(){
		try {
			List<Map<String, Object>> produceAddList=MaterialGroupManagerDao.getProduceAdd();
			act.setOutData("produceAddList", produceAddList);
			act.setForword(FleetSalesUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	public void getFleetFollow(){
		try {
			act.setForword(FleetFollowUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getFleetContract(){
		try {
			act.setForword(FleetContractUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	
	
	
	public void getFleetSalesLog(){
		try {
			List<Map<String, Object>> produceAddList=MaterialGroupManagerDao.getProduceAdd();
			act.setOutData("produceAddList", produceAddList);
			act.setForword(FleetSalesLogUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getFleetPrepar(){
		try {
			act.setForword(FleetPreparCvsUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getActDetaliSales(){
		try {
			StringBuffer str=new StringBuffer("");
			String dealerId=logonUser.getDealerId();
			List<Map<String, Object>> dealerIds=dao.selectAllDealers(dealerId);
			for(int i=0;i<dealerIds.size();i++){
				str.append(dealerIds.get(i).get("DEALER_ID"));
				str.append(",");
			}
			if(dao.selectDealers(dealerId).size()>0){
				str.append(dealerId);
					act.setOutData("mydealerId", str);
			}
			
			
			String orgId=logonUser.getOrgId().toString();
			TmOrgPO po=new TmOrgPO();
			po.setOrgId(Long.parseLong(orgId));
			po.setDutyType(Constant.DUTY_TYPE_LARGEREGION);
			if(dao.select(po).size()==1){
				act.setOutData("orgId", orgId);
			}
			Date date_ = new Date(System.currentTimeMillis());
			Date date1 = new Date(date_.getYear(),date_.getMonth(),01);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String date = dateFormat.format(date_);
			String date2 = dateFormat.format(date1);
			act.setOutData("date", date);
			act.setOutData("date2", date2);
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			act.setOutData("areaList",areaList);
			act.setForword(ActDetailReportUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void getBillDetaliTicket(){
		try {
			
			String orgId=logonUser.getOrgId().toString();
			TmOrgPO po=new TmOrgPO();
			po.setOrgId(Long.parseLong(orgId));
			po.setDutyType(Constant.DUTY_TYPE_LARGEREGION);
			if(dao.select(po).size()==1){
				act.setOutData("orgId", orgId);
			}
			Date date_ = new Date(System.currentTimeMillis());
			Date date1 = new Date(date_.getYear(),date_.getMonth(),01);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String date = dateFormat.format(date_);
			String date2 = dateFormat.format(date1);
			act.setOutData("date", date);
			act.setOutData("date2", date2);
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			act.setOutData("areaList",areaList);
			act.setForword(BillDetailReportUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	public void getFleetActTj(){
		try {
			Long poseId = logonUser.getPoseId();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
			act.setOutData("areaList",areaList);
			act.setForword(FleeActTjUrl);
		}catch (Exception e) {
			BizException e1 = new BizException(act, e,
			ErrorCodeConstant.QUERY_FAILURE_CODE, "报表有误！");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	public void getDealerBillDetaliTicket(){
		StringBuffer str=new StringBuffer("");
		String dealerId=logonUser.getDealerId();
		List<Map<String, Object>> dealerIds=dao.selectAllDealers(dealerId);
		for(int i=0;i<dealerIds.size();i++){
			str.append(dealerIds.get(i).get("DEALER_ID"));
			str.append(",");
		}
		if(dao.selectDealers(dealerId).size()>0){
			str.append(dealerId);
				act.setOutData("mydealerId", str);
		}
		Long poseId = logonUser.getPoseId();
		List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
		act.setOutData("areaList",areaList);
		act.setForword(BillDealerDetailReportUrl);
	}
	
	public void getDealerActDetaliSales(){
		StringBuffer str=new StringBuffer("");
		String dealerId=logonUser.getDealerId();
		List<Map<String, Object>> dealerIds=dao.selectAllDealers(dealerId);
		for(int i=0;i<dealerIds.size();i++){
			str.append(dealerIds.get(i).get("DEALER_ID"));
			str.append(",");
		}
		if(dao.selectDealers(dealerId).size()>0){
			str.append(dealerId);
				act.setOutData("mydealerId", str);
		}
		Long poseId = logonUser.getPoseId();
		List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(poseId.toString());
		act.setOutData("areaList",areaList);
		act.setForword(ActDealerDetailReportUrl);
	}
}
